﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCommon.Prompt;


namespace Baosight.ZNH.ZMCD.UI
{
    public partial class ZMCD14 : EFViewCmes
    {
        public ZMCD14()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 设置部门权限——添加一行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void org_grid_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            UIBaseEntity temp = e.NewObject as UIBaseEntity;
            EIRow _row = temp.GetEIRow();

            if (null != _row)
            {
                _row["fdLimitLevel"] = 1;
                _row["fdLimitLevel1"] = 0;
                _row["fdDirId"] = fdDirId.Value;
                this.org_grid.Columns[this.org_grid.Columns.Count - 2].IsReadOnly = true;
            }
        }


        #region 按钮区域
        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.insertInfo.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;

            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            //List<string> list = new List<string>();
            //this.org_grid.Validate(DataRange.All, out list);
            //this.user_grid.Validate(DataRange.All, out list);
            List<string> list1 = ValidateGrid(org_grid, "fdOrgId");
            if (list1.Count == 0)
            {
                if (this.org_grid.CheckedEIRows.Count > 0)
                {
                    EIInfo info = new EIInfo();
                    info.Set(EIConstants.ServiceNameKey, "ZMCD14");
                    info.Set(EIConstants.MethodNameKey, "insert");
                    EIBlock block = info.AddBlock(this.org_grid.eBlockName);
                    block.SetColumns(this.org_grid.EIBlock.GetColumns());
                    block.SetRows(this.org_grid.CheckedEIRows);
                    base.Submit(info, DoCommitCompleted);
                }
                else
                {
                    this.ValidateMessagePopup("请勾选要添加的记录！");//弹出提示信息
                    this.TempButtonOpen(true);//确定按钮生效
                }               
            }
            else
            {
                string meg = "有错误：";
                foreach (string str in list1)
                {
                    meg += "\r\n部门权限列表的" + str + ";";
                }
                this.ValidateMessagePopup(meg);//弹出提示信息
                this.TempButtonOpen(true);//确定按钮生效
            }

        }

        /// <summary>
        /// 列表的校验
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        private List<string> ValidateGrid(EFGrid grid, string fieldName)
        {
            List<string> list = new List<string>();
            if (grid.EIBlock != null)
            {
                for (int i = 0; i < grid.CheckedEIRows.Count; i++)
                {
                    if (grid.CheckedEIRows[i][fieldName] == null || "".Equals(grid.CheckedEIRows[i][fieldName]))
                    {
                        list.Add("勾选的第" + (i + 1) + "行的第1列不能为空");
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        #endregion


        /// <summary>
        /// 提示错误消息
        /// </summary>
        /// <param name="str"></param>
        private void ValidateMessagePopup(string str)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(str);
            pop.Show(6);

            return;
        }


    }
}
